package com.pay.manager.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.pay.manager.dao.MerchantMapper;
import com.pay.manager.dto.MerchantSearchDTO;
import com.pay.manager.pojo.Merchant;
import com.pay.manager.response.PageDataResult;
import com.pay.manager.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @Title: MerchantServiceImpl
 * @Description:
 * @author: yjw
 * @version: 1.0
 * @date: 2020/6/12 11:04
 */
@Service
public class MerchantServiceImpl implements MerchantService {
	
	@Autowired
	private MerchantMapper merchantDao;

	@Override
	public PageDataResult getMerchantList(MerchantSearchDTO merchantSearch, Integer pageNum, Integer pageSize) {
		 PageDataResult pageDataResult = new PageDataResult();
	        List<Merchant> merchant = merchantDao.getMerchantList(merchantSearch);
	        PageHelper.startPage(pageNum, pageSize);
	        if(merchant.size() != 0){
	            PageInfo<Merchant> pageInfo = new PageInfo<>(merchant);
	            pageDataResult.setList(merchant);
	            pageDataResult.setTotals((int) pageInfo.getTotal());
	        }

	        return pageDataResult;
	}

	@Override
	public Map<String, Object> applyPass(Integer mid,String operationPwd) {
		 Map<String,Object> data = new HashMap();
	        	if(operationPwd.equals("123456")) {
	        		Merchant merchant = merchantDao.selectById(mid);
					merchant.setAuditStatus("2");
					merchantDao.updateById(merchant);
	        		data.put("code",1);
		            data.put("msg","商户审核通过！");
	        	}else {
	        		data.put("code",0);
	                data.put("msg","操作密码有误！");
	                return data;
	        	} 
	        	
	        return data;
	}
	
	@Override
	public Map<String, Object> lower(Integer mid,String state,String operationPwd) {
		 Map<String,Object> data = new HashMap();
		 		if(state.equals("2")){
		 			data.put("code",0);
	                data.put("msg","商户已通过审核！");
	                return data;
		 		}
	        	if(operationPwd.equals("123456")) {
					Merchant merchant = merchantDao.selectById(mid);
					merchant.setAuditStatus("3");
					merchantDao.updateById(merchant);
	        		data.put("code",1);
		            data.put("msg","商户申请审核拒绝！");
	        	}else {
	        		data.put("code",0);
	                data.put("msg","操作密码有误！");
	                return data;
	        	} 
	        	
	        return data;
	}

	@Override
	public Map<String, Object> updateMerchant(Integer mid, String merchantName, String merchantType, String merchantAddress, String username, String mobile, String contactsAddress, String operationPwd) {
		Map<String,Object> data = new HashMap();
		if(operationPwd.equals("123456")) {
			Merchant merchant = merchantDao.selectById(mid);
			merchant.setMerchantName(merchantName);
			merchant.setMerchantType(merchantType);
			merchant.setMerchantAddress(merchantAddress);
			merchant.setUsername(username);
			merchant.setMobile(mobile);
			merchant.setContactsAddress(contactsAddress);
			merchantDao.updateById(merchant);
			data.put("code",1);
			data.put("msg","商户修改信息成功！");
		}else {
			data.put("code",0);
			data.put("msg","操作密码有误！");
			return data;
		}

		return data;
	}

	@Override
	public PageDataResult getMerchantApplyList(MerchantSearchDTO merchantSearch, Integer pageNum, Integer pageSize) {
		 PageDataResult pageDataResult = new PageDataResult();
	        List<Merchant> merchant = merchantDao.getMerchantApplyList(merchantSearch);
	        PageHelper.startPage(pageNum, pageSize);
	        if(merchant.size() != 0){
	            PageInfo<Merchant> pageInfo = new PageInfo<>(merchant);
	            pageDataResult.setList(merchant);
	            pageDataResult.setTotals((int) pageInfo.getTotal());
	        }

	        return pageDataResult;
	}


}
